﻿namespace CloudNinja.Metering.Tests.DataRepository
{
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;

    using CloudNinja.Metering.Data.Models;

    using Xunit;

    public class UpsertTest
    {
        protected string ConnectionString = "Data Source=.; Initial Catalog=cnmb; Integrated Security=SSPI;";

        [Fact]
        public void CanCallUpsertSp()
        {
            var connection = new SqlConnection(this.ConnectionString);

            using (connection)
            {
                connection.Open();
                var testCmd = new SqlCommand(@"Upsert_MeterData", connection)
                                  {
                                      CommandType =
                                          CommandType.StoredProcedure
                                  };
                var insertTypeCmd =
                    new SqlCommand(
                        @"INSERT INTO MeterType ([Id],[Name],[Description],[AggregateType],[UnitOfMeasure], [Weight]) VALUES ('TS4', 'Test Type', 'Test Type', 'sum', 'kb', 3)",
                        connection);
                insertTypeCmd.ExecuteNonQuery();

                var dataPoints = new[]
                                     {
                                         new MeterData
                                             {
                                                 Count = 10,
                                                 MeterTypeId = "TS4",
                                                 TenantId = "DELETETENANT",
                                                 TimePeriod = DateTimeOffset.Now.AddHours(1).DateTime,
                                                 Value = 10
                                             }
                                     };

                var meterDataEntities = dataPoints.ToList();
                if (meterDataEntities.Count <= 0)
                {
                    return;
                }

                var dataTable = new DataTable();
                dataTable.Columns.Add("MeterTypeId", typeof(string));
                dataTable.Columns.Add("TenantId", typeof(string));
                dataTable.Columns.Add("TimePeriod", typeof(DateTime));
                dataTable.Columns.Add("Value", typeof(double));
                dataTable.Columns.Add("Count", typeof(int));

                foreach (var meterData in meterDataEntities)
                {
                    dataTable.Rows.Add(
                        meterData.MeterTypeId,
                        meterData.TenantId,
                        meterData.TimePeriod.Date,
                        meterData.Value,
                        meterData.Count < 1 ? 1 : meterData.Count);
                }

                var param = new SqlParameter("@MeterDataTempTable", SqlDbType.Structured) { Value = dataTable };
                testCmd.Parameters.Add(param);

                testCmd.ExecuteNonQuery();

                var queryCmd = new SqlCommand(@"SELECT * FROM MeterData WHERE TenantId = 'DELETETENANT'", connection);
                var result = queryCmd.ExecuteReader();
                var count = 0;
                while (result.Read())
                {
                    count++;
                }
                result.Close();

                Assert.Equal(1, count);

                var deleteCmd = new SqlCommand(@"DELETE FROM MeterData WHERE TenantId = 'DELETETENANT'", connection);
                deleteCmd.ExecuteNonQuery();

                result = queryCmd.ExecuteReader();
                count = 0;
                while (result.Read())
                {
                    count++;
                }

                result.Close();
                Assert.Equal(0, count);

                var deleteTypeCmd =
                new SqlCommand(@"DELETE FROM MeterType WHERE [Id] = 'TS4'",connection);
                deleteTypeCmd.ExecuteNonQuery();
            }
        }
    }
}
